package test.com.qu.myapplication.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.RemoteException;
import android.util.Log;

import java.util.HashMap;

import test.com.qu.myapplication.INotificationService;

/**
 * Created by Administrator on 2017/3/14.
 * 服务端
 */

public class NoticeUtils {
    public static INotificationService sService = null;
    private static HashMap<Context, ServiceBinder> sConnectionMap = new HashMap<Context, ServiceBinder>();
    public static boolean bindToService(Context context) {//MainActivity中触发了
        return bindToService(context, null);
    }

    public static boolean bindToService(Context context,
                                        ServiceConnection callback) {
        context.startService(new Intent(context, NoticeService.class));
        ServiceBinder sb = new ServiceBinder(callback);
        sConnectionMap.put(context, sb);
        return context.bindService(
                (new Intent()).setClass(context, NoticeService.class), sb, 0);
    }

    public static void unbindFromService(Context context) {//MainActivity中触发了
        ServiceBinder sb = sConnectionMap.remove(context);
        if (sb == null) {
            Log.e("MusicUtils", "Trying to unbind for unknown Context");
            return;
        }
        context.unbindService(sb);
        if (sConnectionMap.isEmpty()) {
            // presumably there is nobody interested in the service at this
            // point,
            // so don't hang on to the ServiceConnection
            sService = null;
        }
    }
//请求通知
    public static void requestNotice(Context context) {
        if (sService != null) {
            try {
                Log.d("NoticeUtils","requestNotice...");

                sService.requestNotice();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        } else {
            context.sendBroadcast(new Intent(
                    NoticeService.INTENT_ACTION_REQUEST));
            Log.d("NoticeUtils","requestNotice,service is null...");
        }
    }




    private static class ServiceBinder implements ServiceConnection {
        ServiceConnection mCallback;

        ServiceBinder(ServiceConnection callback) {
            mCallback = callback;
        }

        @Override
        public void onServiceConnected(ComponentName className,
                                       android.os.IBinder service) {
            sService = INotificationService.Stub.asInterface(service);
            if (mCallback != null) {
                mCallback.onServiceConnected(className, service);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName className) {
            if (mCallback != null) {
                mCallback.onServiceDisconnected(className);
            }
            sService = null;
        }
    }
    public static void tryToShutDown(Context context) {
            context.sendBroadcast(new Intent(
                    NoticeService.INTENT_ACTION_SHUTDOWN));

    }
}
